package com.example.smart_city_0829.mainFragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseFragment;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.Random;


public class NewsFragment extends BaseFragment {

    private CombinedChart combinedChart;
    private LineChart lineChart;
    private PieChart pieChart;
    private HorizontalBarChart horizontalBarChart;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initView() {
        combinedChart = (CombinedChart) rootView.findViewById(R.id.combinedChart);
        lineChart = (LineChart) rootView.findViewById(R.id.LineChart);
        pieChart = (PieChart) rootView.findViewById(R.id.pieChart);
        horizontalBarChart = (HorizontalBarChart) rootView.findViewById(R.id.horizontalBarChart);
    }

    @Override
    protected void initData() {
        Random random = new Random();
        combinedChart.setDrawOrder(new CombinedChart.DrawOrder[]{
                CombinedChart.DrawOrder.LINE,
                CombinedChart.DrawOrder.BAR
        });
        combinedChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        combinedChart.getXAxis().setAxisMinimum(-0.5f);
        combinedChart.getXAxis().setAxisMaximum(12f);
        combinedChart.getXAxis().setGranularity(0f);
        combinedChart.getLegend().setEnabled(false);
        combinedChart.getDescription().setEnabled(false);
        combinedChart.getAxisRight().setEnabled(false);
        ArrayList<Entry> lineEntries1 = new ArrayList<>();
        ArrayList<Entry> lineEntries2 = new ArrayList<>();
        ArrayList<BarEntry> barEntries = new ArrayList<>();
        ArrayList<String> titles = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            lineEntries1.add(new Entry(i, random.nextInt(1000)));
            lineEntries2.add(new Entry(i, random.nextInt(800)));
            barEntries.add(new BarEntry(i, random.nextInt(1200)));
            titles.add(i + 1 + "月");
        }
        LineDataSet lineDataSet1 = new LineDataSet(lineEntries1, "折线一");
        lineDataSet1.setColor(Color.RED);
        LineDataSet lineDataSet2 = new LineDataSet(lineEntries2, "折线二");
        lineDataSet2.setColor(Color.BLUE);
        LineData lineData = new LineData(lineDataSet1, lineDataSet2);
        BarDataSet barDataSet = new BarDataSet(barEntries, "条形一");
        barDataSet.setColor(Color.GREEN);
        BarData barData = new BarData(barDataSet);
        CombinedData combinedData = new CombinedData();
        combinedData.setData(lineData);
        combinedData.setData(barData);
        combinedChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(titles));
        combinedChart.setData(combinedData);

        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lineChart.getDescription().setEnabled(false);
        lineChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(titles));
        lineChart.setData(lineData);

        pieChart.getDescription().setEnabled(false);
        ArrayList<PieEntry> pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(30f, "第1个"));
        pieEntries.add(new PieEntry(50f, "第2个"));
        pieEntries.add(new PieEntry(20f, "第3个"));
        PieDataSet pieDataSet = new PieDataSet(pieEntries, "比例数据");
        pieDataSet.setColors(Color.DKGRAY, Color.CYAN, Color.LTGRAY);
        pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        PieData pieData = new PieData(pieDataSet);
        pieData.setValueFormatter(new PercentFormatter(pieChart));
        pieChart.setDrawHoleEnabled(false);
        pieChart.setUsePercentValues(true);
        pieChart.setEntryLabelColor(Color.BLACK);
        pieChart.setData(pieData);

        horizontalBarChart.getDescription().setEnabled(false);
        horizontalBarChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        horizontalBarChart.getAxisLeft().setEnabled(false);
        horizontalBarChart.getXAxis().setGranularity(0f);
        horizontalBarChart.getXAxis().setAxisMinimum(0f);
//        horizontalBarChart.getXAxis().setAxisMaximum();
//        horizontalBarChart.getXAxis().setLabelCount(8);
        ArrayList<BarEntry> barEntries1 = new ArrayList<>();
        ArrayList<BarEntry> barEntries2 = new ArrayList<>();
        ArrayList<String> title1 = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            barEntries1.add(new BarEntry(i, random.nextInt(200)));
            barEntries2.add(new BarEntry(i, random.nextInt(400)));
            title1.add(i+ "条");
        }
        BarDataSet barDataSet1 = new BarDataSet(barEntries1, "第一组数据");
        barDataSet1.setColor(Color.MAGENTA);
        BarDataSet barDataSet2 = new BarDataSet(barEntries2, "第二组数据");
        barDataSet2.setColor(Color.LTGRAY);
        BarData barData1 = new BarData(barDataSet1, barDataSet2);
        barData1.setBarWidth(0.3f);
        barData1.groupBars(0f,0.3f,0.05f);
        horizontalBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(title1));
        horizontalBarChart.setData(barData1);
    }

    @Override
    protected void initEvent() {

    }
}